package com.example.demo.zhxy;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.example.demo.MapSort;
import com.example.demo.zhxy.entity.SendMsgEntity;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import jodd.bean.BeanTemplateParser;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.io.FileUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class SignKit {
	private @NonNull ZhxyApp app;
	
	public String post(String url, Map<String, Object> params) throws IOException {
		
		// 公共参数
		String time = (System.currentTimeMillis() / 1000) + "";
		params.put("timestamp", time);
		params.put("objType", app.getObjType());
		params.put("openAppID", app.getOpenAppId());
		params.put("objectid", app.getObjectId());

		String stringMap = FileUtil.readString(new File("d:/params.temp"));// MapSort.toStringMap(params);
		
		System.out.println(stringMap + "&key=" + app.getAppSecre());
		String sign = SecureUtil.md5(stringMap + "&key=" + app.getAppSecre()).toUpperCase();
		
		SendMsgEntity se = new SendMsgEntity();
		se.setTimestamp(Convert.toLong(time));
		se.setObjType(app.getObjType());
		se.setOpenAppID(app.getOpenAppId());
		se.setObjectid(app.getObjectId());
		se.setSign(sign);
		val data = new com.example.demo.zhxy.entity.SendMsgEntity.Data();
		data.setContent("dfdfdfd");
		data.setMsgtype("text");
		data.setWxdepartid("");
		data.setWxuserid("campus_4758|campus_4759");
		se.setData(data);
		
		
		stringMap = new BeanTemplateParser().parseWithBean(stringMap, se);
		log.info("编辑参数字符串:" + stringMap);
		
		log.info("计算sign:" + sign);
		String paramsStr = stringMap + "&sign=" + sign;
		String urlFinal = url + "?" + paramsStr;
		log.info("最终url:" + urlFinal);
		String body = FileUtil.readString(new File("d:/json.temp"));// MapSort.toStringMap(params);
		
		
		String post = HttpUtil.post(urlFinal, body);
		
		System.out.println(UnicodeUtil.toString(post));

		return null;
	}

	public String get(String url, Map<String, Object> params) {
		String urlFinal = this.getUrlFinal(url, params);
		HttpResponse send = HttpRequest.get(urlFinal).send();
		return UnicodeUtil.toString(send.body());
	}

	/**
	 * 根据请求url跟请求参数拼接为完整url请求
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	private String getUrlFinal(String url, Map<String, Object> params) {
		// 公共参数
		String time = (System.currentTimeMillis() / 1000) + "";
		params.put("timestamp", time);
		params.put("objType", app.getObjType());
		params.put("openAppID", app.getOpenAppId());
		params.put("objectid", app.getObjectId());

		String stringMap = MapSort.toStringMap(params);
		log.info("编辑参数字符串:" + stringMap);
		String temp = "data[content]=dfdfdfd&data[msgtype]=text&data[wxdepartid]=&data[wxuserid]=campus_4758|campus_4759&";
		System.out.println(temp + stringMap + "&key=" + app.getAppSecre());
		String sign = SecureUtil.md5(temp + stringMap + "&key=" + app.getAppSecre()).toUpperCase();
		log.info("计算sign:" + sign);
		String paramsStr = stringMap + "&sign=" + sign;
		String urlFinal = url + "?" + paramsStr;
		log.info("最终url:" + urlFinal);

		return urlFinal;
	}

	@Data
	@AllArgsConstructor
	public static class ZhxyApp {
		private String openAppId;
		private String appSecre;

		private String objectId;
		private Long objType;// 不知道什么东西,demo中为2
	}

}
